package com.zbl.zblproject.core.controller;

import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.entity.SmallUnit;
import com.zbl.zblproject.core.mapper.SmallUnitMapper;
import com.zbl.zblproject.core.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/25
 */
@RestController
@Api(tags={"单位接口"})
@RequestMapping("/small-unit")
public class UnitController {

    @Autowired
    private SmallUnitMapper smallUnitMapper;

    @GetMapping("/unit")
    @ApiOperation("获取所有的小单位")
    public Result smallUntis(){
        return ResultUtil.success("成功!", smallUnitMapper.selectList(null));
    }

    @PostMapping("/add")
    @ApiOperation("添加一个单位")
    public Result addUnit(@ApiParam("单位名称") @RequestParam("name") String name){
        try {
            boolean result = 1 == smallUnitMapper.insert(new SmallUnit(name));
            if (result) {
                return ResultUtil.success("添加成功!");
            } else {
                return ResultUtil.error("添加失败!");
            }
        }catch(Exception e){
            return ResultUtil.error("错误!");
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除一个单位")
    public Result deleteUnit(@ApiParam("单位ID") @PathVariable int id){
        boolean result = 1 == smallUnitMapper.deleteById(id);
        if(result){
            return ResultUtil.success("删除成功!");
        }else{
            return ResultUtil.error("删除失败!");
        }
    }

}
